package cn.shujuhai.common.pluging.data;

import lombok.Data;

@Data
public class ResultVo<T> {
    // 状态码
    private int code;
    // 状态信息
    private String msg;
    // 返回数据对象
    private T data;

    // 成功的默认构造方法，返回数据
    public ResultVo(T data) {
        this(ResultCode.SUCCESS, data);
    }

    // 使用状态码和数据构造结果
    public ResultVo(ResultCode resultCode, T data) {
        this.code = resultCode.getCode();
        this.msg = resultCode.getMsg();
        this.data = data;
    }

    public ResultVo(String msg, T data) {
        this.msg = msg;
        this.data = data;
    }

    // 无数据的成功响应
    public static <T> ResultVo<T> success() {
        return new ResultVo<>(ResultCode.SUCCESS, null);
    }

    // 有数据的成功响应
    public static <T> ResultVo<T> success(T data) {
        return new ResultVo<>(ResultCode.SUCCESS, data);
    }

    public static <T> ResultVo<T> loginSuccess() {
        return new ResultVo<>(ResultCode.LOGIN_SUCCESS, null);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    // 失败响应
    public static <T> ResultVo<T> failure(ResultCode resultCode) {
        return new ResultVo<>(resultCode, null);
    }

    // 失败响应带数据
    public static <T> ResultVo<T> failure(ResultCode resultCode, T data) {
        return new ResultVo<>(resultCode, data);
    }

    public static <T> ResultVo<T> failure(String msg) {
        return new ResultVo<>(msg, null);
    }

}
